package com.nuaa.yupicturebackend.controller;

import com.nuaa.yupicturebackend.common.BaseResponse;
import com.nuaa.yupicturebackend.common.ResultUtils;
import com.nuaa.yupicturebackend.model.dto.comment.CommentAddRequest;
import com.nuaa.yupicturebackend.model.entity.Comment;
import com.nuaa.yupicturebackend.model.vo.CommentVO;
import com.nuaa.yupicturebackend.service.CommentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 图片评论
 */
@RestController
@RequestMapping("/comments")
public class CommentController {
	@Resource
	private CommentService commentService;

	/**
	 * 保存评论
	 */
	@PostMapping
	public BaseResponse<Boolean> createComment(@RequestBody CommentAddRequest commentAddRequest) {
		Boolean flag = commentService.saveComment(commentAddRequest);
		return ResultUtils.success(flag);
	}

	/**
	 * 获取图片的评论
	 */
	@GetMapping("/picture")
	public BaseResponse<List<CommentVO>> getRootComments(@RequestParam("pictureId") Long pictureId) {
		List<CommentVO> commentVOList = commentService.getRootCommentsByImage(pictureId);
		return ResultUtils.success(commentVOList);
	}

	/**
	 * 搜索评论
	 */
	@GetMapping("/search")
	public BaseResponse<List<Comment>> searchComments(@RequestParam String keyword) {
		return ResultUtils.success(commentService.searchComments(keyword));
	}

	/**
	 * 删除评论
	 */
	@PostMapping("/delete")
	public BaseResponse<Boolean> deleteComment(@RequestParam("commentId") Integer commentId) {
		Boolean flag = commentService.deleteComment(commentId);
		return ResultUtils.success(flag);
	}

}